#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility? -----------#
#--------------------------------------------------------------------------------------------------------------------#
#---------------------- D2 Variation of electoral discrimination across local contexts  -----------------------------#
#--------------------------------------------------------------------------------------------------------------------#

rm(list=ls())
setwd(".../...")
load("d_nr15_gde")

# ------------------------------------ Data preparation

## Build factors
cols <- c( "nonswissname", "precumulated", "list_id", 
"bfssex","partypos", "bfscantno.y", "bfsincumbency", "bfsresidence_code", 
"cand_id", "bfsan_sprache", "rel_catholic", "bfsraum_degurba", "origname_lang", 
"origname_cath", "age_c3")
d_nr15_gde[cols] <- lapply(d_nr15_gde[cols], factor)

## Build numeric variables
d_nr15_gde[c("age", "dd_cons", "soc_aid", "crime_stgb", "crime_betmg", "crime_aug", "party_mun_svp", "empl_1_perc", 
"empl_2_perc", "empl_3_perc")] <- lapply(d_nr15_gde[c("age", "dd_cons", "soc_aid", "crime_stgb", "crime_betmg", 
"crime_aug", "party_mun_svp", "empl_1_perc", "empl_2_perc", "empl_3_perc")], as.numeric)

## Center all numerical variables
d_nr15_gde[c("age_c", "rel_pos_c", "num_listpos_c", "prop_ogn_foreign_tot_c",  
"bev_ausl_perc_2015_c", "foreign_born_perc_2015_c", "bev_ausl_change_c", "foreign_born_change_c", 
"soc_aid_c", "crime_tot_c", "party_mun_svp_c", "empl_1_perc_c", 
"empl_2_perc_c", "empl_3_perc_c", "dd_cons_c")] <- 
lapply(d_nr15_gde[c("age", "rel_pos", "num_listpos", "prop_ogn_foreign_tot",  
"bev_ausl_perc_2015", "foreign_born_perc_2015", "bev_ausl_change", "foreign_born_change", 
"soc_aid", "crime_tot", "party_mun_svp", "empl_1_perc", 
"empl_2_perc", "empl_3_perc", "dd_cons")], 
scale, center=TRUE, scale=FALSE)

d_nr15_gde$nonswissname <- factor(d_nr15_gde$nonswissname, 
  levels=c(0,1), 
  labels=c("Swiss", "Non-Swiss name"))

d_nr15_gde <- d_nr15_gde[!d_nr15_gde$partypos == 4, ]

d_nr15_gde_m <- d_nr15_gde[!d_nr15_gde$bfscantno.y == 20, ] #Drop canton of Thurgau (because no data available for naturalizations)

d_nr15_gde_mp <- d_nr15_gde_m[!is.na(d_nr15_gde_m$pos_mean),] 
d_nr15_gde_mp <- d_nr15_gde_mp[ ,!colSums(is.na(d_nr15_gde_mp))] 

d_nr15_gde_mn <- d_nr15_gde_m[!is.na(d_nr15_gde_m$npv_rel),] 
d_nr15_gde_mn <- d_nr15_gde_mn[ ,!colSums(is.na(d_nr15_gde_mn))] 

## Check how much moderating municipal level variables correlate (Factor variables excluded: "bfsraum_degurba", 
## "bfsan_sprache", "rel_catholic")

cor(d_nr15_gde_m[, c("dd_cons", "prop_ogn_foreign_tot", 
"bev_ausl_perc_2015", "foreign_born_perc_2015", "foreign_born_change", "soc_aid",
"crime_tot", "party_mun_svp", "empl_1_perc", "empl_2_perc", "empl_3_perc")]) 
# Strong correlations as expected between: (1) Number of naturalized citizens since 1991 (prop_ogn_foreign_tot) & 
# Percent foreign population (bev_ausl_perc_2015) & Percent of foreign born (foreign_born_perc_2015); 
# (2) Share of votes for SVP (party_mun_svp) & Conservative voting in direct democratic ballots (dd_cons)

## For the current models only include of these strongly correlated variables: prop_ogn_foreign_tot, dd_cons. 
## Also, exclude interaction with Position of party ballot on the right-left axis (partypos) in models with dd_cons. 

d_nr15_gde_m <- as.data.frame(d_nr15_gde_m)

# ------------------------------------ Models negative preference votes

# Negative preference votes

m1a <- lmer(npv_rel ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos*nonswissname + num_listpos_c + 
bfsraum_degurba + bfsan_sprache + rel_catholic+ soc_aid_c + crime_tot_c  + dd_cons +
nonswissname*prop_ogn_foreign_tot_c + bfscantno.y + 
(1 | bfsresidence_code) + (1 | list_id), data=d_nr15_gde_mn)
summary(m1a)

# Variable share of naturalized population not centered for figure

m1b <- lmer(npv_rel ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos*nonswissname + num_listpos_c + 
bfsraum_degurba + bfsan_sprache + rel_catholic+ soc_aid_c + crime_tot_c  + dd_cons +
nonswissname*prop_ogn_foreign_tot + bfscantno.y + 
(1 | bfsresidence_code) + (1 | list_id), data=d_nr15_gde_mn)
summary(m1b)

# ------------------------------------ Figure negative preference votes

colors <- c("black", "grey")

ef <- effect("nonswissname:prop_ogn_foreign_tot", m1b)
summary(ef)
ef <- as.data.frame(ef)
f_negint <- ggplot(ef, aes(prop_ogn_foreign_tot, fit, color=nonswissname, fill=nonswissname)) + 
geom_line(size=1) + 
geom_ribbon(aes(ymin=lower, ymax=upper), size=0.1, alpha=0.5) + 
theme_bw() + labs(y ="Predicted negative preference votes", x= "Share of naturalized population", color="Candidate name") + 
scale_colour_manual(values = colors, aesthetics = c("colour", "fill")) + guides(fill=FALSE); f_negint

# ------------------------------------ Models positive preference votes

m2a <- lmer(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos*nonswissname + num_listpos_c + 
bfsraum_degurba + bfsan_sprache + rel_catholic+ soc_aid_c + crime_tot_c  + dd_cons +
nonswissname*prop_ogn_foreign_tot_c + bfscantno.y + 
(1 | bfsresidence_code) + (1 | list_id), data=d_nr15_gde_mp)
summary(m2a)

m2b <- lmer(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos*nonswissname + num_listpos_c + 
bfsraum_degurba + bfsan_sprache + rel_catholic+ soc_aid_c + crime_tot_c  + dd_cons +
nonswissname*prop_ogn_foreign_tot + bfscantno.y + 
(1 | bfsresidence_code) + (1 | list_id), data=d_nr15_gde_mp)
summary(m2b)

# ------------------------------------ Figure positive preference votes

ef <- effect("nonswissname:prop_ogn_foreign_tot", m2b)
summary(ef)
ef <- as.data.frame(ef)
f_posint <- ggplot(ef, aes(prop_ogn_foreign_tot, fit, color=nonswissname, fill=nonswissname)) + 
geom_line(size=1) + 
geom_ribbon(aes(ymin=lower, ymax=upper), size=0.1, alpha=0.5) + 
theme_bw() + labs(y ="Predicted positive preference votes", x= "Share of naturalized population", color="Candidate name") + 
scale_colour_manual(values = colors, aesthetics = c("colour", "fill")) + guides(fill=FALSE); f_posint

# ------------------------------------ Figure 4 Appendix: Predicted negative preference votes and positive preference votes, 
#by candidate name and the share of naturalized citizens in the municipality

int_loc <- ggarrange(f_negint, f_posint, 
labels = c("", ""),
ncol = 2, nrow = 1)
int_loc

png("Figures/int_loc.png", width = 8.5, height = 4, units = 'in', res = 300)
plot(int_loc)
dev.off()

# ------------------------------------ Table Table 17: The effect of candidate names (Swiss vs. non-Swiss) on negative and positive preference
#votes, local level variation

custom.name.1 <- c("Intercept", 
                   "Relative position on ballot", "Incumbent",
                   "Pre-cumulated", "Male", "Age: 30-50 years ", "Age: 50+ years", 
                   "Party position = center", "Party position = left", "non-Swiss name",
                   "Number of candidtes on ballot",
                   "Degree of urbanization: intermediate density", 
                   "Degree of urbanization: thinly populated", "Language region municipality: French", 
                   "Language region municipality: Italian", "Language region municipality: Romantsch", 
                   "Religion = Catholic", "Social assistance rate", "Crime rate", "Conservative attitudes", 
                   "Share of naturalized citizens", "non-Swiss name x party position = centre", 
                   "non-Swiss name x party position = left", 
                   "non-Swiss name x proportion of naturalized citizens")

texreg(list(m1a, m2a), booktabs=TRUE,
       caption="The effect of candidate names (Swiss vs. non-Swiss) on negative and positive preference votes, local level variation",
       omit.coef = "cant",
       custom.coef.names = custom.name.1,
       #reorder.coef = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 12, 13),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))
